package com.android.namecard.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.AdapterView.OnItemClickListener;

import com.android.namecard.R;
import com.android.namecard.entity.Namecard;
import com.android.namecard.model.NamecardDBHelper;
import com.android.namecard.util.NamecardViewAdapter;

public class WelcomeActivity extends Activity {

	
	private GridView gv;
	private int[] icons={R.drawable.new_namecard,R.drawable.scan,
            R.drawable.send,R.drawable.exit};
    private String[] items={"新建我的名片","浏览名片","蓝牙发送名片","退出软件"};
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.welcome);
        
        gv = (GridView)findViewById(R.id.namecard_grid_content); 
        /* 使用告定义的NamecardViewAdapter设置GridView里面的item内容 */
        NamecardViewAdapter adapter=new NamecardViewAdapter(this,items,icons);
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(new OnItemClickListener(){
    		public void onItemClick(AdapterView<?> arg0, View arg1, int index,
    				long arg3) {
    			// TODO Auto-generated method stub
    			if(index == 0){
    				//新建名片
    				Intent intent = new Intent(WelcomeActivity.this, MakeCardActivity.class);
    				startActivity(intent);
    			}else if(index == 1){
    				//查看名片
    				Intent intent = new Intent(WelcomeActivity.this, NamecardListActivity.class);
    				startActivity(intent);
    			}else if(index == 2){
    				//蓝牙发送名片
    				Intent intent = new Intent(WelcomeActivity.this, SendTypeActivity.class);
    				startActivity(intent);
    			}else if(index == 3){
    				//退出程序
    				new AlertDialog.Builder(WelcomeActivity.this)
				    .setTitle("温馨提醒")//标题
				    .setMessage("请问您确定要退出吗？")//设置内容
				    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							System.exit(0);
						}
					})
					 .setNegativeButton("取消", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							
						}
					}).create().show();
			    }
    		}
    		
        });
        
    }

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode == KeyEvent.KEYCODE_BACK){
			new AlertDialog.Builder(WelcomeActivity.this)
		    .setTitle("温馨提醒")//标题
		    .setMessage("请问您确定要退出吗？")//设置内容
		    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					System.exit(0);
				}
			})
			 .setNegativeButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			}).create().show();
			return true;
		}
		return false;
	}
	
}
